package dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import pojo.Comment;
import pojo.Merchant;
import pojo.Order;
import dao.MerchantDao;

@Repository(value="merchantDao")
public class MerchantDaoImpl implements MerchantDao {
	@PersistenceContext(name="un")
	private EntityManager manager;
	
	@Override
	public List<Merchant> findAll() {
		List<Merchant> merchantList = manager.createQuery("select distinct f.merchant from Food f").getResultList();
		return merchantList;
	}

	@Override
	public List<Merchant> findByType(String typeId) {
		List<Merchant> merchantList = (List<Merchant>)manager.createQuery("select distinct f.merchant from Food f where f.categories.id =:id")
				.setParameter("id", typeId)
				.getResultList();
		System.out.println(merchantList.size());
		return merchantList;
	}

	@Override
	public Merchant findById(String id) {
		Merchant merchant = manager.find(Merchant.class, id);
		return merchant;
	}

	@Override
	public List<Order> getOrderByMerchantId(String id) {
		String jpql = "select o from Order o INNER JOIN fetch o.merchant where o.merchant.id =:id";
        List<Order> orderList = manager.createQuery(jpql).setParameter("id", id).getResultList();
        return orderList;
	}

}
